//
//  LessonPreviewGridViewCell.m
//  Cachalot
//
//  Created by Adam Cue on 7/1/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "LessonPreviewGridViewCell.h"

#define LESSON_PREVIEW_VIEW_FRAME         CGRectMake(24, 0, 208, 277)
#define TITLE_LABEL_FRAME                 CGRectMake(0, 177, 256, 22)

@interface LessonPreviewGridViewCell (Private)
#pragma mark SetupMethods
- (void)setupTitleLabel;
- (void)setupLessonPreviewView;
@end

@implementation LessonPreviewGridViewCell

@synthesize titleLabel = _titleLabel;
@synthesize asyncLessonPreviewView = _asyncLessonPreviewView;
@synthesize superViewController;

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier andSuperViewController:(UIViewController <LessonPreviewViewDelegate> *) vc{
   self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier];
   if (self) {
      self.backgroundColor = [UIColor clearColor];
      self.selectionStyle = AQGridViewCellSelectionStyleNone;
      self.contentView.backgroundColor = [UIColor clearColor];
      self.superViewController = vc;
      [self setupTitleLabel];
      [self setupLessonPreviewView];
   }
   return self;
}

- (void)dealloc{
   self.titleLabel = nil;
   self.asyncLessonPreviewView = nil;
   self.superViewController = nil;
   
   [super dealloc];
}

#pragma mark SetupMethods
- (void)setupTitleLabel{
   _titleLabel = [[UILabel alloc] initWithFrame:TITLE_LABEL_FRAME];
   self.titleLabel.textAlignment = UITextAlignmentCenter;
   self.titleLabel.font = [UIFont systemFontOfSize:12];
   self.titleLabel.textColor = [UIColor whiteColor];
   self.titleLabel.backgroundColor = [UIColor clearColor];
   [self.contentView addSubview:self.titleLabel];
}

- (void)setupLessonPreviewView{
   _asyncLessonPreviewView = [[LessonPreviewView alloc] initWithFrame:LESSON_PREVIEW_VIEW_FRAME index:0];
	[self.asyncLessonPreviewView setDelegate:self.superViewController];
	self.asyncLessonPreviewView.backgroundColor = [UIColor clearColor];
	[self.contentView addSubview:self.asyncLessonPreviewView];
}

#pragma mark ExternalMethods
+ (void)fillCell:(LessonPreviewGridViewCell *)cell forLesson:(NSDictionary *)lesson atIndex:(NSInteger)index{
   cell.asyncLessonPreviewView.index = index;
   if([lesson objectForKey:@"thumbnail_path"] != nil){
      [cell.asyncLessonPreviewView loadImageFromURL:[NSURL URLWithString:[lesson objectForKey:@"thumbnail_path"]] withDefaultImage:nil];
	}
   else{
      [cell.asyncLessonPreviewView setDisplayImage:[UIImage imageNamed:@"default_thumb"]];
   }
	cell.titleLabel.text = [lesson objectForKey:@"name"];
}

@end
